package com.fk.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fk.reggie.common.BaseContext;
import com.fk.reggie.common.R;
import com.fk.reggie.domain.OrderDetail;
import com.fk.reggie.domain.Orders;
import com.fk.reggie.domain.User;
import com.fk.reggie.dto.OrdersDto;
import com.fk.reggie.service.OrderDetailService;
import com.fk.reggie.service.OrdersService;
import com.fk.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private UserService userService;
    //请求 URL: http://localhost:8080/order/submit
    //请求方法: POST

    /**
     * 添加订单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        ordersService.submit(orders);
        return R.success("添加购物车成功");
    }

    //请求 URL: http://localhost:8080/order/userPage?page=1&pageSize=1
    //请求方法: GET   用户端的分页
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(Integer page, Integer pageSize) {
        Long userId = BaseContext.getThreadLocal();
        Page<Orders> ordersPage = new Page<>(page, pageSize);

        Page<OrdersDto> ordersDtoPage = new Page<>();


        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(Orders::getUserId, userId);
        ordersService.page(ordersPage, ordersLambdaQueryWrapper);

        //复制属性
        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");

        List<Orders> ordersList = ordersPage.getRecords();

        List<OrdersDto> ordersDtos;
        ordersDtos = ordersList.stream().map(orders -> {
            OrdersDto ordersDto = new OrdersDto();
            Long id = orders.getId();
            LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId, id);
            List<OrderDetail> orderDetailList = orderDetailService.list(orderDetailLambdaQueryWrapper);
            ordersDto.setOrderDetails(orderDetailList);
            return ordersDto;
        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(ordersDtos);
        return R.success(ordersDtoPage);
    }

    //请求 URL: http://localhost:8080/order/page?page=1&pageSize=10&number=1&beginTime=2023-03-20%2000%3A00%3A00&endTime=2023-04-19%2023%3A59%3A59
    //请求方法: GET
    @GetMapping("/page")
    public R<Page<OrdersDto>> page(@RequestParam Map<String, Object> map) {
        log.info(map.toString());
        Integer page = Integer.valueOf((String) map.get("page"));
        Integer pageSize = Integer.valueOf((String) map.get("pageSize"));
        Long number = null;
        String beginTime = null;
        String endTime = null;
        if (map.get("number") != null) {
            number = Long.valueOf((String) map.get("number"));
        }
        if (map.get("beginTime") != null) {
            beginTime = (String) map.get("beginTime");
        }
        if (map.get("endTime") != null) {
            endTime = (String) map.get("endTime");
        }

        Page<Orders> ordersPage = new Page<>(page, pageSize);

        Page<OrdersDto> ordersDtoPage = new Page<>(page, pageSize);


        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.like(number != null, Orders::getNumber, number)
                .between(beginTime != null && endTime != null, Orders::getOrderTime, beginTime, endTime);
        ordersService.page(ordersPage, ordersLambdaQueryWrapper);

        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");

//        Long userId = BaseContext.getThreadLocal();
//        User user = userService.getById(userId);


        List<OrdersDto> ordersDtoList;
        List<Orders> records = ordersPage.getRecords();
        ordersDtoList = records.stream().map(orders -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders, ordersDto);
            User user = userService.getById(orders.getUserId());
            ordersDto.setUserName(user.getName());
            return ordersDto;
        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(ordersDtoList);

        return R.success(ordersDtoPage);
    }

    //请求 URL: http://localhost:8080/order
    //请求方法: PUT   修改订单状态
    @PutMapping
    public R<String> updateStatus(@RequestBody Orders orders) {
        ordersService.updateById(orders);
        return R.success("修改状态成功");
    }

}
